<?php
// include constants
include "./includes/vars.php";
include "./includes/containers.php";
include "newvars.php";
$pathToRoot = "./"; 		// or "";

?>

<?php
include "header.php";
?>


<?php
// content php code goes here

$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die("Cannot connect to database server");

mysql_select_db($dbname)
  or Die("Database not found.");


function getDwonloadBookLink1($link){
$pathToRoot = "./"; 		// or "";

	return '<a href="' . $pathToRoot . 'index.php?act=download&BID=' . $link . '" class="link_download">Download this book</a>';
	//return '<A HREF="index.php?act=download&BID=' . $link . '"><img src="images/downloadThisBook.gif" BORDER=0></A>';
}

//added by walid.elswaify on 2 May 2008 to support new UI
function getBookImageLink($imageUrl, $altMsg) {
return '<img src="' . $imageUrl . '" alt="' . $altMsg . '" width="45" height="70"/>';
}

// content management


if(isset($_GET['authID'])){

    // get author ID and find books written and list alphabetically
    $sql = "SELECT BA_BOOK_ID FROM bk_auth WHERE BA_AUTH_ID = " . $_GET['authID'] . ' LIMIT ';

    if (!isset($_GET['page'])){
        $sql .= '0';
    }
    else{
        $sql .=  (($_GET['page']-1)*$bktoshownum);
    }
    
    $sql .= ", " . $bktoshownum;

    
    
    
    
    $bookslist = "";
    if ($result = mysql_query($sql)){
    
        while ($row = mysql_fetch_row($result)){

            // extract the book's information and display the list
            if (trim($row[0] != "")){
            
                $bsql = "SELECT B_NAME,  B_IMAGE_URL from books WHERE B_ID = " . $row[0];
                if ($bresult = mysql_query($bsql)){
                    $brow = mysql_fetch_row($bresult);

                    if (trim($brow[1]) != ""){
                        $bookslist .= '<p><a href="' . $pathToRoot . 'chapters.php?bkID=' . $row[0] . '"><img border="0" width="100" height="150" src="' . $websitename . $brow[1] . '"><br>';
                        $bookslist .= $brow[0] . '</a><br>';                                                
                        $bookslist .= getDwonloadBookLink1($row[0]);
                    }
                }
                
            }
            
            

        }
    
    }
    else{
        $bookslist = "Could not retrieve this author's books list.";
    }
    
    
    
            $sql = "SELECT BA_BOOK_ID FROM bk_auth WHERE BA_AUTH_ID = " . $_GET['authID'];
            if ($result = mysql_query($sql)){
                $numrecords = mysql_num_rows($result);

            }
            else{
                $numrecords = 5;
            }

            $numpages = round($numrecords/$bktoshownum);

            if (($numpages-($numrecords/$bktoshownum))<0){

                $numpages += 1;
            }

            if (!isset($_GET['page'])){
                $pagenumvalue = 1;
            }
            else{
                $pagenumvalue = $_GET['page'];
            }


            $pagenum = "page " . $pagenumvalue . " of " . $numpages;

            $prevlink = "";
            
            if ($pagenumvalue > 1){
                $prevlink = '<A HREF="' . $pathToRoot . 'books.php?authID=' . $_GET['authID'] . '&page=' . ($pagenumvalue-1) . '"><img src="' . $websitename . '/images/ib_previous.gif" border="0"></A>'  ;
            }


            $nextlink = "";
            if ($numpages > $pagenumvalue){
                $nextlink = '<A HREF="' . $pathToRoot . 'books.php?authID=' . $_GET['authID'] . '&page=' . ($pagenumvalue+1) . '"><img src="' . $websitename . '/images/ib_next.gif" border="0"></A>'  ;
            }
            
            
            $bookslist .= '<P align="right">' . $prevlink . ' ' . $pagenum . ' '. $nextlink . '</P>';


    
    $authorname = "";
    // get the author's name
    $asql = "SELECT * FROM authors WHERE A_ID = " . $_GET['authID'];
    if ($aresult = mysql_query($asql)){

    
        $arow = mysql_fetch_row($aresult);
        $authorname = $arow[1] . ' ' . $arow[2];
    
    }
    
    

    
    $authorview = str_replace("<!--AUTHORNAME-->", $authorname, $authorview);

    $authorview = str_replace("<!--CONTENT-->", $bookslist, $authorview);
    $content = $authorview;
    



}


else{

$content ='
<script language="javascript">
<!--

document.location="index.php";


-->
</script>
';


}









// prepare and send final file
/*
commented by walid.elswaify
if (file_exists($templatefile)){
    $output = implode ("", file($templatefile));
    $output = str_replace("<!--CONTENT-->", $content, $output);    $output = str_replace("<!--SIDEBAR-->", $sidebar, $output);
    print $output;

    
}
else{
    print "ERROR";
}
*/

mysql_close ($link);

		
?>
<!-- content html code goes here-->
<td rowspan="2" class="content_center" valign="top">
  
            <div class="content_body">
				Books of <?php print $authorname;?> </br>
				
				<?php print $bookslist;?>
				
				
            </div>
        </td>
<!-- end of content html code-->



<?php
include "footer.php";
?>